/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordFilter;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/**
 *
 * @author LTTDNQ
 */
public class RecordHighScore implements  RecordComparator {
    /*
     * The RecordStore used for storing the game scores.
     */

    private RecordStore recordStore = null;
    /*
     * The player name to use when filtering.
     */
    private int[] _arrInt;
    private static RecordHighScore instance;

    public int[] getArrInt() {
        return _arrInt;
    }

    public void setArrInt(int[] x) {
        this._arrInt = x;
    }


    private RecordHighScore() {
        openRecordScore();
        _arrInt = new int[]{0,0,0,0,0};
    }

    public static synchronized RecordHighScore getInstance() {
        if (instance == null) {
            instance = new RecordHighScore();
        }
        
        return instance;
    }
    /*
     * Part of the RecordFilter interface.
     */

    public int compare(byte[] rec1, byte[] rec2) {
        ByteArrayInputStream bais1 = new ByteArrayInputStream(rec1);
        DataInputStream inputStream1 = new DataInputStream(bais1);
        ByteArrayInputStream bais2 = new ByteArrayInputStream(rec2);
        DataInputStream inputStream2 = new DataInputStream(bais2);
        int score1 = 0;
        int score2 = 0;
        try {
            // Extract the scores.
            score1 = inputStream1.readInt();
            score2 = inputStream2.readInt();
        } catch (EOFException eofe) {
            System.out.println(eofe);
            eofe.printStackTrace();
        } catch (IOException eofe) {
            System.out.println(eofe);
            eofe.printStackTrace();
        }

        // Sort by score
        if (score1 < score2) {
            return RecordComparator.PRECEDES;
        } else if (score1 > score2) {
            return RecordComparator.FOLLOWS;
        } else {
            return RecordComparator.EQUIVALENT;
        }
    }

    public void openRecordScore() {
        //
        // Create a new record store for this example
        //
        try {
            recordStore = RecordStore.openRecordStore("scores", true);
        } catch (RecordStoreException rse) {
            System.out.println(rse);
            rse.printStackTrace();
        }
    }

    public void addScore(int x) {
        //
        // Each score is stored in a separate record, formatted with
        // the score, followed by the player name.
        //
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream outputStream = new DataOutputStream(baos);
        try {
            // Push the score into a byte array.
            outputStream.writeInt(x);
            // Then push the player name.
        } catch (IOException ioe) {
            System.out.println(ioe);
            ioe.printStackTrace();
        }

        // Extract the byte array
        byte[] b = baos.toByteArray();
        // Add it to the record store
        try {
            recordStore.addRecord(b, 0, b.length);
        } catch (RecordStoreException rse) {
            System.out.println(rse);
            rse.printStackTrace();
        }
    }

    /**
     * A helper method for the printScores methods.
     */
    private void printScoresHelper(RecordEnumeration re) {
        try {
            int i = 0;
            while (re.hasNextElement() && i < 5) {
                int id = re.nextRecordId();
                ByteArrayInputStream bais = new ByteArrayInputStream(recordStore.getRecord(id));
                DataInputStream inputStream = new DataInputStream(bais);
                try {
                    int score = inputStream.readInt();
                    _arrInt[i] = score;
                } catch (EOFException eofe) {
                    System.out.println(eofe);
                    eofe.printStackTrace();
                }
                i++;
            }
        } catch (RecordStoreException rse) {
            System.out.println(rse);
            rse.printStackTrace();
        } catch (IOException ioe) {
            System.out.println(ioe);
            ioe.printStackTrace();
        }
    }

    /**
     * This method prints all of the scores sorted by game score.
     */
    public void printScores() {
        try {
            // Enumerate the records using the comparator implemented
            // above to sort by game score.
            RecordEnumeration re = recordStore.enumerateRecords(null,this,true);
            printScoresHelper(re);
        } catch (RecordStoreException rse) {
            System.out.println(rse);
            rse.printStackTrace();
        }
    }
}